<template>
  <div class="chat-content">
    <div class="header-box">
      <button @click="$router.push('/detail')">点击跳转</button>
      <van-button type="primary" @click="handleInfo">请求示例</van-button>
      <p>{{ info.content }}</p>
    </div>
    <div class="chat-message-list">
      <div ref="chatScrollRef" class="messages-content-scroll">
        <div v-for="(item, index) in 100" :key="index">{{ item }}</div>
        <div class="chat-message-r-item">
          <div class="message-content-wrapper">
            <div class="message-meta">
              <div class="message-author-contain">
                <span class="long-date">11:22:02</span>
              </div>
              <div class="chat-message-content">
                <div class="message-content">
                  如果你的样式需要做根据视口大小来调整宽度，这个脚本可以将你CSS中的px单位转化为vw，1vw等于1/100视口宽度。
                </div>
              </div>
            </div>
            <!-- 头像 -->
            <div class="chat-avatar">
              <div class="chat-avatar-img">
                <img src="https://img.yzcdn.cn/vant/cat.jpeg" alt="cat" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="editor-box">
      <div id="toolbar">
        <button class="ql-image"></button>
      </div>
      <div id="editor"></div>
      <van-button class="save-btn" type="primary" @click="handleSave">发送</van-button>
    </div>
  </div>
</template>

<script setup>
import Quill from 'quill';
import 'quill/dist/quill.snow.css';
import baseApi from '@/api/baseApi';
import { nextTick, onMounted, ref } from 'vue';
/**
 * 请求示例
 */
const info = ref('');
const handleInfo = () => {
  baseApi.getInfo().then((result) => {
    info.value = result.data;
  });
};

const chatScrollRef = ref(null);
nextTick(() => {
  const scrollEl = chatScrollRef.value;
  // behavior: 'smooth'
  scrollEl.scrollTo({ top: scrollEl.scrollHeight });
});

const quill = ref(null);
onMounted(() => {
  // 富文本编辑器
  quill.value = new Quill('#editor', {
    theme: 'snow',
    placeholder: '请输入内容...',
    modules: {
      toolbar: '#toolbar',
      clipboard: {
        matchers: [[Node.ELEMENT_NODE, handleCustomMatcher]],
      },
    },
  });
});
/**
 * 处理粘贴只需要文本内容
 * @param {*} node
 * @param {*} delta
 */
const handleCustomMatcher = (node, delta) => {
  console.log(node, delta);
  delta.ops = delta.ops.map((op) => {
    return {
      insert: op.insert,
    };
  });
  return delta;
};
/**
 * 保存
 */
const handleSave = () => {
  const content = quill.value.getContents();
  content.ops.forEach((op) => {
    if (op.insert) {
      if (op.insert.image) {
        console.log(op.insert.image);
      }
    }
  });
  console.log(content);
};
</script>

<style lang="scss" scoped>
.chat-content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  .chat-message-list {
    width: 100%;
    height: 100%;
    flex: 1;
    position: relative;
    background-color: #f3f3f3;
    // background-color: antiquewhite;
    .messages-content-scroll {
      position: absolute;
      top: 0px;
      left: 0px;
      right: 0px;
      bottom: 0px;
      overflow: scroll;
      .chat-message-r-item {
        padding: 5px 10px 0;
        overflow-y: hidden;
        box-sizing: border-box;
        width: 100%;
        .message-content-wrapper {
          display: flex;
          justify-content: flex-end;
          align-items: flex-start;
          overflow: hidden;
          box-sizing: border-box;
          padding-bottom: 6px;
          .message-meta {
            width: 100%;
            max-width: 85%;
            justify-content: flex-end;
            align-content: flex-end;
            align-items: flex-end;
            display: flex;
            flex-direction: column;
            .long-date {
              font-size: 12px;
            }
            .chat-message-content {
              display: flex;
              justify-content: flex-end;
              position: relative;
              &::after {
                position: absolute;
                width: 10px;
                height: 10px;
                z-index: 1;
                content: ' ';
                transform: rotate(45deg);
                background: #a9ea7a;
                box-sizing: border-box;
                border-bottom-color: transparent !important;
                border-right-color: transparent !important;
                border-top-left-radius: 2px;
                border: 1px solid var(--el-border-color-light);
                top: 10px;
                right: 0;
              }
              .message-content {
                max-width: 80%;
                min-height: 35px;
                padding: 8px;
                border-radius: 4px;
                border: 1px solid #f0f0f0;
                background-color: #a9ea7a;
                color: #333;
                font-size: 14px;
                overflow: hidden;
                word-break: break-word;
              }
            }
          }
          .chat-avatar {
            display: inline-block;
            .chat-avatar-img {
              width: 35px;
              height: 35px;
              display: inline-flex;
              justify-content: center;
              align-items: center;
              overflow: hidden;
              background-color: #fff;
              vertical-align: top;
              border-radius: 50%;
              cursor: pointer;
              flex: 0 0 35px;
              margin: 2px 10px 0;
              margin-top: 15px;
              img {
                max-width: 100%;
                max-height: 100%;
                box-sizing: border-box;
                vertical-align: middle;
                border-style: none;
              }
            }
          }
        }
      }
    }
  }
  .editor-box {
    min-height: 120px; /* px-to-viewport-ignore */
    background-color: #f3f3f3;
    .save-btn {
      position: absolute;
      bottom: 10px;
      right: 20px;
    }
  }
  #editor {
    height: 50px;
    border: 1px solid #ccc;
    min-height: 96px; /* px-to-viewport-ignore */
  }
}
</style>
